//
//  ListViewController.m
//  ViperDemo
//
//  Created by Leeesven on 17/5/17.
//  Copyright © 2017年 Leeesven. All rights reserved.
//

#import "ListViewController.h"
#import "UpcomingDisplayData.h"
#import "UpcomingDisplaySection.h"
#import "UpcomingDisplayItem.h"

static NSString* const ListEntryCellIdentifier = @"ListEntryCell";

@interface ListViewController()

@property (nonatomic, strong) UpcomingDisplayData* data;

@property (nonatomic, strong) UIView *noContentView;

@end

@implementation ListViewController

-(void)viewDidLoad{
    [self configureView];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self.eventHandler updateView];
}

#pragma mark - private
-(void)configureView{
    self.view.backgroundColor = [UIColor whiteColor];
}

-(void)reloadEntries{
    [self.tableView reloadData];
}

-(UIView*)noContentView{
    if (_noContentView == nil) {
        _noContentView = [[UIView alloc] initWithFrame:self.view.frame];
        _noContentView.backgroundColor = [UIColor whiteColor];
    }
    return _noContentView;
}

#pragma mark - ListViewInterface
-(void)showUpcomingDisplayData:(UpcomingDisplayData *)data{
    self.data = data;
    [self reloadEntries];
}

-(void)showNoContentMessage{
    self.view = self.noContentView;
}

#pragma mark - UITableViewDelegate and DataSource Methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.data.sections.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    UpcomingDisplaySection *upcomingSection = self.data.sections[section];
    return upcomingSection.items.count;
}

-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    UpcomingDisplaySection *upcomingSection = self.data.sections[section];
    return upcomingSection.name;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UpcomingDisplaySection *section = self.data.sections[indexPath.section];
    UpcomingDisplayItem *item = section.items[indexPath.row];
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ListEntryCellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ListEntryCellIdentifier];
    }
    cell.textLabel.text = item.title;
    cell.detailTextLabel.text = item.dueDay;
    cell.imageView.image = [UIImage imageNamed:section.imageName];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

@end
